package com.wt.wechatTools.tools;

import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MyRequest {
    public static final String TAG = "MyRequest";
//    public static final String proUrl = "https://api.gd97.xyz/ListManagementAPI/";
    public static final String proUrl = "http://192.168.0.95/ListManagementAPI/";
    public static final String register_token = "123456789";
//  public static final MediaType FORM_CONTENT_TYPE  = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
// android 官方文档 网络安全配置   https://developer.android.google.cn/training/articles/security-config.html
// 关于android:networkSecurityConfig的添加   https://lycorisradiata.cn/174.html
// 关于android:networkSecurityConfig的添加    https://www.it1352.com/1677479.html

    /**
     * API测试方法
     */
    public static void apiTest() {
        OkHttpClient okHttpClient = new OkHttpClient();
        final okhttp3.Request request = new okhttp3.Request.Builder()
                .url(proUrl + "test333")
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse: " + response.body().string());
            }
        });
    }

    /**
     * 公共的post方法
     *  @param apiName     api接口名称
     * @param requestBody 参数对象
     * @return
     */
    public static void apiPost(String apiName, RequestBody requestBody) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
//        RequestBody requestBody = new FormBody.Builder()
//                .add("username", "tttttt")
//                .add("pwd", "ttttttttt")
//                .add("register_token", register_token)
//                .build();

        Request request = new Request.Builder()
                .url(proUrl + apiName)
                .post(requestBody)
//                .addHeader("Content-Type", "text/plain; charset=utf-8")
//                .addHeader("User-Agent", "PostmanRuntime/7.22.0")
//                .addHeader("Accept", "*/*")
//                .addHeader("Cache-Control", "no-cache")
//                .addHeader("Postman-Token", "f4803fbd-8773-4788-a86e-3843a9f39469")
//                .addHeader("Host", "47.244.131.97:8081")
//                .addHeader("Accept-Encoding", "gzip, deflate, br")
//                .addHeader("Cookie", "PHPSESSID=v182qe7rv0tk5mqqmk470ia2m6")
//                .addHeader("zh-CN,zh;q=0.9", "zh-CN,zh;q=0.9")
//                .addHeader("application/x-www-form-urlencoded","charset=utf-8")
//                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, response.protocol() + " " + response.code() + " " + response.message());
                Headers headers = response.headers();
                for (int i = 0; i < headers.size(); i++) {
                    Log.d(TAG, headers.name(i) + ":" + headers.value(i));
                }
//解决OKHTTP返回Response乱码问题https://blog.csdn.net/qq_37492806/article/details/104560454
                Log.d(TAG, "onResponse: " + response.body().string());
            }
        });
    }


}
